<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all"/>
</head>
<body class="childrenBody">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal layui-form layui-row" onsubmit="return false" id="form">
        <fieldset>
            <div class='form-group'>
                <label class='col-md-2 control-label'>名字</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='名字' type='text' name='name' id='name'>
                </div>
            </div>
            <div class='form-group layui-form-item'>
                <label class='col-md-2 control-label'>地区</label>
                <div class='col-md-10'>
                    <div class='layui-inline'>
                        <select id="quiz1" name="province" lay-filter="quiz1" class="form-control">
                            <option value="">请选择省</option>
                        </select>
                    </div>
                    <div class='layui-inline'>
                        <select id="quiz2" name="city" lay-filter="quiz2" class='layui-inline'>
                            <option value="">请选择市</option>
                        </select>
                    </div>
                    <div class='layui-inline'>
                        <select id="quiz3" name="area" class='layui-inline'>
                            <option value="">请选择县/区</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>地址</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='地址' type='text' name='address' id='address' >
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>律师事务所执业证件号</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='律师事务所执业证件号' type='text' name='licensenumber' id='licensenumber' >
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>律师事务所执业证件</label>
                <div class='col-md-10'>
                    <button type="button" class="layui-btn" id="practisingcertificate_upload">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>预览</label>
                <div class='col-md-10'>
                    <div class="layui-upload-list" id="practisingcertificate_show"></div>
                </div>
            </div>
            <input type="hidden" name="practisingcertificate" id="practisingcertificate">
            <div class='form-group'>
                <label class='col-md-2 control-label'>介绍</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='介绍' type='text' name='introduce' id='introduce' >
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">擅长领域</label>
                <div class=" col-md-10">
                    <div id="fuxuankuang">

                    </div>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>入驻律师人数</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='入驻律师人数' type='text' name='numberoflawyers' id='numberoflawyers' >
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>成立年限</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='成立年限' type='text' name='yearsofestablishment' id='yearsofestablishment' >
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>logo</label>
                <div class='col-md-10'>
                    <button type="button" class="layui-btn" id="logo_upload">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>预览</label>
                <div class='col-md-10'>
                    <div class="layui-upload-list" id="logo_show"></div>
                </div>
            </div>
            <input type="hidden" name="logo" id="logo">
            <div class='form-group'>
                <label class='col-md-2 control-label'>主任律师</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='主任律师' type='text' name='chiefcounsel' id='chiefcounsel' >
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>注册资金</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='注册资金' type='text' name='registeredcapital' id='registeredcapital' >
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>排序</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='排序' type='text' name='order' id='order' >
                </div>
            </div>
             <div class='form-group'>
                 <label class='col-md-2 control-label'>联系电话</label>
                 <div class='col-md-10'>
                     <input class='form-control' placeholder='联系电话' type='text' name='phone' id='phone'>
                 </div>
             </div>
            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="btn btn-primary" onclick="location.href='mfflLawfirmList.html'">返回</button>
                        <button class="btn btn-primary" type="button" onclick="add()">
                            <i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
            </div>

        </fieldset>
    </form>
</div>
<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
        var form = layui.form;
        $.ajax({
            type: 'get',
            url: '/Communitys/Level',
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                $.each(data.datas, function (index, item) {
                    $('#quiz1').append(new Option(item.PROVINCE_NAME, item.PROVINCE_CODE));// 下拉菜单里添加元素
                });
                layui.form.render("select");
            }
        });
    });

    //联动
    layui.use(['form'], function () {
        var form = layui.form;
        form.on('select(quiz1)', function (data) {
            var val = data.value;
            $.ajax({
                type: 'get',
                url: '/Communitys/Level2?code=' + val,
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    $('#quiz2').html("<option value=\"\">请选择市</option>");
                    $('#quiz3').html("<option value=\"\">请选择县/区</option>");
                    $.each(data.datas, function (index, item) {
                        $('#quiz2').append(new Option(item.CITY_NAME, item.CITY_CODE));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");
                }
            });
        });
        form.on('select(quiz2)', function (data) {
            var val = data.value;
            $.ajax({
                type: 'get',
                url: '/Communitys/Level3?code=' + val,
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    $('#quiz3').html("<option value=\"\">请选择县/区</option>");
                    $.each(data.datas, function (index, item) {
                        $('#quiz3').append(new Option(item.AREA_NAME, item.AREA_CODE));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");
                }
            });
        });
    });

    initData();

    function initData() {
        $.ajax({
            type: 'get',
            url: '/client/mfflTypes/getLawyerType',
            async: false,
            success: function (data) {
                var lawyerType = data.datas;
                for (var i = 0; i < lawyerType.length; i++) {
                    $('#fuxuankuang').append('<input type="checkbox"  name="field" value="' + lawyerType[i].id + '" title="' + lawyerType[i].name + '" >')
                }
                //var form = layui.form
                //form.render();
            }
        });
    }


    var pro = window.location.protocol;
    var host = window.location.host;
    var domain = pro + "//" + host;

    var layedit, index;
    layui.use(['layedit', 'upload'], function () {
        layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/files/layui?domain=' + domain + "&token=" + localStorage.getItem("token"),
                type: 'post'
            }
        });
        index = layedit.build('content');
    });


    //执业证件上传
    var practisingcertificate_array = [];  //律师事务所执业证件
    layui.use('upload', function () {
        var upload = layui.upload;
        //多图片上传
        upload.render({
            elem: '#practisingcertificate_upload', //绑定id
            url: '/mffl/uploadimagefile',//上传图片
            multiple: true,
            done: function (res) {
                //如果上传成功
                if (practisingcertificate_array.length >= 1) {
                    layer.msg("最多上传1张图片");
                    return;
                }
                if (res.errCode == 200) {
                    $('#practisingcertificate_show').append('<img src="/statics' + res.datas.max + '"  title="点击删除" height="120" width="120" class="layui-upload-img" onclick="delMultipleImgs(this)">')
                    //追加图片成功追加文件名至图片容器
                    practisingcertificate_array.push(res.datas.max);
                    //追加到隐藏标签中
                    $('#practisingcertificate').val(practisingcertificate_array);
                } else {
                    layer.msg(res.message);//提示信息
                }
            }
        });
    });


    //执业证件删除
    function delMultipleImgs(this_img) {
        //获取下标
        var subscript = $("#practisingcertificate_show img").index(this_img);
        //删除图片
        this_img.remove();
        //删除数组
        practisingcertificate_array.splice(subscript, 1);
        //重新排序
        practisingcertificate_array.sort();
        $('#practisingcertificate').val(practisingcertificate_array);
        return;
    }


    //logo上传
    var logo_array = [];  //logo
    layui.use('upload', function () {
        var upload = layui.upload;
        //多图片上传
        upload.render({
            elem: '#logo_upload', //绑定id
            url: '/mffl/uploadimagefile',//上传图片
            multiple: true,
            done: function (res) {
                //如果上传成功
                if (logo_array.length >= 1) {
                    layer.msg("最多上传1张图片");
                    return;
                }
                if (res.errCode == 200) {
                    $('#logo_show').append('<img src="/statics' + res.datas.max + '"  title="点击删除" height="120" width="120" class="layui-upload-img" onclick="delMultipleImgs(this)">')
                    //追加图片成功追加文件名至图片容器
                    logo_array.push(res.datas.max);
                    //追加到隐藏标签中
                    $('#logo').val(logo_array);
                } else {
                    layer.msg(res.message);//提示信息
                }
            }
        });
    });


    //logo删除
    function delMultipleImgs(this_img) {
        //获取下标
        var subscript = $("#logo_show img").index(this_img);
        //删除图片
        this_img.remove();
        //删除数组
        logo_array.splice(subscript, 1);
        //重新排序
        logo_array.sort();
        $('#logo').val(logo_array);
        return;
    }

    //时间
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#yearsofestablishment',
            type: 'datetime'
        });
    });

    function add() {
        $('#form').bootstrapValidator();
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }
        var arr = new Array();
        $("input:checkbox[name='field']:checked").each(function (i) {
            arr[i] = $(this).val();
            return;
        })
        var formdata = $("#form").serializeObject();
        formdata.field = arr.join(",");


        if (formdata.name == "") {
            layer.msg("名字 不能为空")
            return;
        }
        if (formdata.name == "") {
            layer.msg("address 不能为空")
            return;
        }
        if (formdata.licensenumber == "") {
            layer.msg("律师事务所执业证件号 不能为空")
            return;
        }
        if (formdata.introduce == "") {
            layer.msg("介绍 不能为空")
            return;
        }
        if (formdata.numberoflawyers == "") {
            layer.msg("入驻律师人数 不能为空")
            return;
        }
        if (formdata.yearsofestablishment == "") {
            layer.msg("成立年限 不能为空")
            return;
        }
        if (formdata.chiefcounsel == "") {
            layer.msg("主任律师 不能为空")
            return;
        }
        if (formdata.chiefcounsel == "") {
            layer.msg("注册资金 不能为空")
            return;
        }
        if (formdata.order == "") {
            layer.msg("排序 不能为空")
            return;
        }
        if (formdata.phone == "") {
            layer.msg("联系电话 不能为空")
            return;
        }
        if (formdata.practisingcertificate == "") {
            layer.msg("执业证书不能为空")
            return;
        }

        if (formdata.logo == "") {
            layer.msg("logo 不能为空")
            return;
        }

        if (formdata.province == "") {
            layer.msg("省 不能为空")
            return;
        }

        if (formdata.city != "") {
            formdata.comid = formdata.city;
            formdata.areaname = $("#quiz1 option:selected").text() + $("#quiz2 option:selected").text();
            if (formdata.area != "") {
                formdata.comid = formdata.area;
                formdata.areaname = $("#quiz1 option:selected").text() + $("#quiz2 option:selected").text() + $("#quiz3 option:selected").text();
            }
        } else {
            formdata.comid = formdata.province;
            formdata.areaname = ("#quiz1 option:selected").text();
        }


        $.ajax({
            type: 'post',
            url: '/mfflLawfirms',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata),
            success: function (data) {
                layer.msg("添加成功", {shift: -1, time: 1000}, function () {
                    location.href = "mfflLawfirmList.html";
                });
            }
        });
    }

</script>
</body>
</html>
